package
{
	import com.greensock.TweenLite;
	
	import events.SlideEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import utils.XMLHelper;
	
	public class SlideShowDemo extends Sprite
	{

		private var slideShow:SlideShow;
		private var arr:Array = ["images/fly.jpg","images/mouse.jpg","images/country.jpg"];
		private var index:int = 0;

		private var controlBar:ControlBar;

		private var timer:Timer;

		private var tip:Tip;
		private var list:XMLList;
		public function SlideShowDemo()
		{
			// 加载XML文件
			XMLHelper.loadXML("slideshow-data.xml", onXMLLoaded);		
		}
		/**
		 * XML加载完成 
		 * @param _xml
		 * 
		 */		
		private function onXMLLoaded(_xml:XML):void
		{
			list = _xml.image;
			trace(list);
			init();
		}
		/**
		 * 
		 * 
		 */		
		private function init():void
		{			
			slideShow = new SlideShow();
			this.addChild( slideShow );
			
			slideShow.switchImage( "images/fly.jpg");
			
			//stage.addEventListener(MouseEvent.CLICK, onClick);
			tip = new Tip();
			this.addChild( tip );
			
			controlBar = new ControlBar();
			this.addChild( controlBar );
			
			controlBar.y = stage.stageHeight - controlBar.height;
			
			controlBar.addEventListener(SlideEvent.SLIDE, onAction);
			
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
					
			tip.updateTip("I am a tip!!!!!");
			tip.y = stage.stageHeight - controlBar.height;
			
			stage.addEventListener(MouseEvent.MOUSE_OVER, onStageMouseOver);
			stage.addEventListener(MouseEvent.MOUSE_OUT, onStageMouseOut);
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function onStageMouseOut(event:MouseEvent):void
		{
			TweenLite.to( tip, 0.6, {y: stage.stageHeight - controlBar.height });
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function onStageMouseOver(event:MouseEvent):void
		{
			if( stage.mouseY < controlBar.y )
			{
				TweenLite.to( tip, 0.6, {y: controlBar.y - tip.height});
			}
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function onTimer(event:TimerEvent):void
		{
			forward();
		}
		
		protected function onAction(event:SlideEvent):void
		{
			trace( event.action );
			switch( event.action )
			{
				case SlideEvent.BACK:
					back();
					break;
				case SlideEvent.FORWARD:
					forward();
					break;
				case SlideEvent.PLAY:
					timer.start();
					break;
				case SlideEvent.PAUSE:
					timer.stop();
					break;
			}
		}
		/**
		 * 下一张 
		 * 
		 */		
		private function forward():void
		{
			trace("forward");
			if( index >=  list.length() - 1)
			{
				index = 0;
			}else
				index++;
			
			var url:String = list[index].@src;
			slideShow.switchImage( url );
			tip.updateTip(list[index].@desc);
		}
		/**
		 * 上一张 
		 * 
		 */		
		private function back():void
		{
			trace("back");
			if( index == 0 )
			{
				index = list.length() - 1;
			}else
				index--;
			
			var url:String = list[index].@src;
			slideShow.switchImage( url );
			tip.updateTip(list[index].@desc);
		}
		
		
		/*protected function onClick(event:MouseEvent):void
		{
			if( index < arr.length - 1 )
			{
				index++;
			}else 
			{
				index = 0;
			}
				
			var url:String = arr[index];
			slideShow.switchImage( url );
		}*/
	}
}